<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta HTTP-EQUIV="expires" CONTENT="0">

    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>

    <link rel="stylesheet" href="../Admin/static/css/font.css">
    <link rel="stylesheet" href="../Admin/static/css/weadmin.css">

    <link rel="Shortcut Icon" href="../images/favicon.ico"/>
    <link rel="stylesheet" href="../src/css/layui.css">
    <link rel="stylesheet" href="src/css/layui.css">
    <script src="../src/layui.js"></script>
    <script src="src/layui.js"></script>
    <script src="../js/jquery-3.2.1.js"></script>
    <script src="js/jquery-3.2.1.js"></script>

    <!--必须放最后-->
    <script src="../js/setting.js"></script>
    <script src="js/setting.js"></script>
</head>
<body>

<header>

</header>


<main>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>权限管理</legend>
    </fieldset>
    <form class="layui-form " action="" method="post">
        <div class="layui-form-item ">
            <div class=" layui-input-inline">
                <input type="text" name="s" placeholder="关键字" autocomplete="off" class="layui-input">
            </div>
            <div class=" layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="search">模糊查询</button>
            </div>
        </div>
    </form>
    <div class="layui-btn-group">
        <!--//刷新-->
        <a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);"> <i class="layui-icon">&#x1002;</i>
        </a>
        <!-- //添加-->
        <a class="layui-btn layui-btn-sm" onclick="WeAdminShow('添加用户','./add.html')" href="javascript:;">
            <i class="layui-icon">&#xe654;</i>
        </a>
        <!--//删除-->
        <button class="layui-btn layui-btn-sm"
                onclick="ToolBar.remove('teachTable','/user/delete/list');">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
    <table class="layui-table" lay-data="" id="teachTable" lay-filter="teachTable"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-primary layui-btn-mini" lay-event="save">保存</a>
        <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
    </script>

</main>


</body>

<script>
    layui.config({
        base: '../static/js/'
        , version: '101100'
    }).use('admin');
    layui.use(['table', 'jquery', 'form'], function () {
        var form = layui.form;
        var $ = layui.$;
        var table = layui.table;
        var index;
        $("#teachTable").ready(function () {
            index = layer.msg('加载中', {
                icon: 16
                , shade: 0.01
            });
        });


        var tableParam = { //其它参数在此省略
            id: 'teachTable',
            elem: '#teachTable',
            url: '/user/list',
            /* data:[{"answer":"小白鼠","id":10001,"level":0,"mail":"amoxu@qq.com","password":"11a9d9f0794dafe80ab4ed4ef6145215","question":"who","tid":0,"user":"misty"},{"alterTime":"2018-03-21 17:59:05","createTime":"2018-03-21 17:58:09","id":10010,"level":1,"mail":"asasa","password":"sadsd","tid":0,"user":"sasa"}],
 */
            page: true,
            limits: [10, 20, 50, 100, 300],
            limit: 10,
            response: {
                statusName: 'status' //数据状态的字段名称，默认：code
                , statusCode: 0 //成功的状态码，默认：0
                , msgName: 'msg' //状态信息的字段名称，默认：msg
                , countName: 'count' //数据总数的字段名称，默认：count
                , dataName: 'data' //数据列表的字段名称，默认：data
            }
            /*            ,height:'full-336',*/
            , cols: [[
                {checkbox: true},
                {field: 'id', width: '130', title: 'ID'},
                {field: 'tid', edit: true, width: '130', title: '教师号'},
                {field: 'level', edit: false, width: '130', title: '权限', event: 'place', templet: '#practiceTmpl'},
                {field: 'user', edit: true, width: 130, title: '用户名'},
                {field: 'mail', edit: true, width: 200, title: '邮箱'},
                {field: 'question', edit: false, width: 200, title: '安全问题'},
                {field: 'createTime', edit: false, width: 200, title: '创建时间'},
                {field: 'alterTime', edit: false, width: 200, title: '上次修改时间'},
                /*{field: 'anser', edit: true, width: '130', title: '找回问题'},*/
                {title: '操作', width: 130, align: 'center', toolbar: '#barDemo'}
            ]]
            , done: function (res, curr, count) {
                console.log(res);
                layer.close(layer.index);
            }
        }
        var tableObj = table.render(tableParam);
        form.render();
        form.on('submit(search)', function (data) {
            if (data.field.s.length < 1) {
                layer.msg("请输入查询内容。");
                return false;
            }
            layer.msg('加载中', {icon: 16, shade: 0.01});
            tableParam.url = "/user/search?s=" + data.field.s
            table.render(tableParam);
            return false;
        });


        table.on('edit(teachTable)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段

            layer.msg('[教师: ' + data.teacher + '] ' + field + ' 更改为：' + value + "<br>确认修改后点击保存。");
        });
        table.on('tool(teachTable)', function (obj) {
            var data = obj.data;
            var tr = obj.tr;
            if (obj.event === 'save') {
                //layer.msg('保存：<br>'+ JSON.stringify(data));
                //ajax上传服务器
                //$('.layui-laypage-btn').click();
                $.ajax({
                    type: 'post'
                    , url: "/user/update"
                    , data: data
                    , dataType: 'json'
                    , contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
                    , success: function (res) {
                        if (res["status"] == 1) {
                            layer.msg(res['msg'], {
                                icon: 2,
                                time: 1000
                            });
                        } else {
                            layer.msg(res['msg'], {
                                icon: 1,
                                time: 1000
                            });
                        }
                        setTimeout("$('.layui-laypage-btn').click();", 2000)
                    }
                    , error: function (res) {
                        layer.msg(res['msg']);
                        setTimeout("$('.layui-laypage-btn').click();", 2000)
                    }
                });
            } else if (obj.event === 'del') {
                layer.confirm('真的删除该行么', function (sure) {
                    $.ajax({
                        type: 'post'
                        , url: "/user/delete"
                        , data: data
                        , dataType: 'json'
                        , contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
                        , success: function (res) {
                            if (res["status"] == 1) {
                                layer.msg(res['msg'], {
                                    icon: 2,
                                    time: 1000
                                });
                            } else {
                                layer.msg(res['msg'], {
                                    icon: 1,
                                    time: 1000
                                });
                            }
                        }
                        , error: function (res) {
                            layer.msg(res['msg']);
                        }
                    });
                    obj.del();
                    layer.close(sure);
                    $('.layui-laypage-btn').click();
                });
            } else if (obj.event === 'place') {
                //显示下拉框框
                type_select.show({
                    //设置当前选择的id
                    value: obj.data.type_id,
                    //下拉选择数据
                    data: [{
                        value: 0,
                        text: '管理员'
                    }, {
                        value: 1,
                        text: '超级管理员'
                    }]
                }, this, function (e) {
                    //回调函数 返回结果
                    obj.data.level = e.value;

                    layer.msg("确认修改请保存");

                    obj.update(obj.data);
                    form.render();
                });
            }
            return false;
        });
    });
</script>
<script type="text/html" id="practiceTmpl">


    {{#  if(d.level === '1' || d.level === 1){ }}
    超级管理员
    {{#  } else { }}
    管理员
    {{#  } }}
    <i class="layui-icon table-select-icon"></i>
</script>
</html>